﻿using UnityEngine;
using UnityEngine.EventSystems;

public class CameraController : MonoBehaviour
{
    private Vector3 touchStart;
    public float zoomOutMin = 1;
    public float zoomOutMax = 8;
    public float minX = -10; // 最小X坐标
    public float maxX = 10; // 最大X坐标
    public float minY = -10; // 最小Y坐标
    public float maxY = 10; // 最大Y坐标

    void Update()
    {
        MyInputSystem.MouseInfo curMouseInfo = MyInputSystem.Instance.curMouse;
        if (curMouseInfo.pressing && !curMouseInfo.longPressing)
        {
            MyInputSystem.MouseInfo startMouseInfo = MyInputSystem.Instance.pressStartMouse;
            if (!startMouseInfo.isOnUI)
            {
                Vector3 direction = startMouseInfo.worldPosition - curMouseInfo.worldPosition;
                MyInputSystem.Instance.mainCamera.transform.position += direction;
            }
        }

        Vector3 camPos = Camera.main.transform.position;
        camPos.x = Mathf.Clamp(camPos.x, minX, maxX);
        camPos.y = Mathf.Clamp(camPos.y, minY, maxY);
        Camera.main.transform.position = camPos;

        // 检测鼠标滚轮缩放
        if (MyInputSystem.Instance.isZooming)
        {
            Camera.main.orthographicSize += MyInputSystem.Instance.zoomDelta;
        }

        Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, zoomOutMin, zoomOutMax);
        RestrainToPosition();
    }

    public void RestrainToPosition()
    {
        Vector3 newPosition = Camera.main.transform.position;
        newPosition.x = Mathf.Clamp(newPosition.x, minX, maxX);
        newPosition.y = Mathf.Clamp(newPosition.y, minY, maxY);
        Camera.main.transform.position = newPosition;
    }

    private void LateUpdate()
    {
        RestrainToPosition();
    }
}